﻿using System;
using System.IO;
using System.Text;


namespace Lon.Server
{

    internal class TjwxRtAlarmProcess : DataProcessProvider
    {
        private NetNodeManager csmNodeManager;
        private FrontServerNode frontServer;
        public TjwxRtAlarmProcess(NetNodeManager nodeManager,  FrontServerNode frontServer)
        {
            this.csmNodeManager = nodeManager;
            this.frontServer = frontServer;
        }

        protected override void ProcessDataPackage(DataFrame rPkg)
        {
            if (rPkg.DataBuf == null) return;
            if (rPkg.DataBuf.Length < 3) return ;
            if (rPkg.DataBuf[0] == 0) return ;
            this.ProcessRtAlarm(rPkg);

        }

        protected override void ProcessCommandPackage(DataFrame pkg)
        {
            MemoryStream ms = new MemoryStream(pkg.DataBuf);
            BinaryReader br = new BinaryReader(ms);
            UInt32 startTime = br.ReadUInt32();
            UInt32 endTime = br.ReadUInt32();
            MemoryStream[] res = DataStoreService.ReadAlarm(pkg.SrcId, startTime, endTime);
            MemoryStream resStream = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(resStream);
            bw.Write((byte)0x42);
            bw.Write((byte)res.Length);
            for (int i = 0; i < res.Length; i++)
            {
                res[i].WriteTo(resStream);
            }
            DataFrame df = new DataFrame(pkg.SrcId, pkg.DstId, DataDir.StationData, false, false, false, (byte)pkg.MajorDataType, pkg.MinorDataType, resStream.ToArray());

            this.frontServer.SendMessage(df);

        }
        private void ProcessRtAlarm(DataFrame rPkg)
        {
           
            if (rPkg.MinorDataType == 0x06)
            {
                DealFaultNotify(rPkg);
            }
            else
            {
                DealAlarm(rPkg);
            }

        }

        private void DealAlarm(DataFrame df)
        {
            MemoryStream ms = new MemoryStream(df.DataBuf);
            BinaryReader br = new BinaryReader(ms);
            br.ReadByte();
            int count = br.ReadInt16();
            for (int i = 0; i < count; i++)
            {
                UInt32 startTime = br.ReadUInt32();
                ms.Seek(5, SeekOrigin.Current);
                int strLen=br.ReadByte();
                byte[] strBuf = br.ReadBytes(strLen);
                String devName = Encoding.ASCII.GetString(strBuf);
                DataStoreService.SaveAlarm(df.SrcId, df.MinorDataType, startTime, devName, df.DataBuf);

            }
        }

        private void DealFaultNotify(DataFrame df)
        {
            MemoryStream ms = new MemoryStream(df.DataBuf);
            BinaryReader br = new BinaryReader(ms);
            br.ReadByte();
            int count = br.ReadInt16();
            for (int i = 0; i < count; i++)
            {
                UInt32 startTime = br.ReadUInt32();
                DataStoreService.SaveAlarm(df.SrcId, df.MinorDataType, startTime, "故障通知", df.DataBuf);
            }
        }
    }
}

